var t = require("../../../@babel/runtime/helpers/defineProperty"), a = require("../../../@babel/runtime/helpers/createForOfIteratorHelper"), e = require("../../../wxParse/wxParse"), i = getApp();

Page({
    data: {
        time: 864e5,
        group_id: 0,
        deliver_id: 0,
        group: {},
        deliver: {},
        hand_goods_obj: {},
        hand_goods_id: 0,
        cart_ids: [],
        disabledInp: !1,
        shareShow: !1,
        shareOptions: [ {
            name: "生成海报",
            icon: "poster"
        }, {
            name: "分享给好友",
            icon: "wechat",
            openType: "share"
        } ],
        themeColor: "#f14e4e"
    },
    onLoad: function(t) {
        var a = decodeURIComponent(t.scene);
        if (a) {
            var e = [];
            e = a.split("&");
            for (var i = 0; i < e.length; i++) {
                var s;
                "gid" == (s = e[i].split("="))[0] && this.setData({
                    group_id: s[1]
                }), "did" == s[0] && this.setData({
                    deliver_id: s[1]
                });
            }
        }
        t.buyingid && this.setData({
            group_id: t.buyingid
        }), t.deliver_id && this.setData({
            deliver_id: t.deliver_id
        }), this.getData();
    },
    onChangeTime: function(t) {
        this.setData({
            timeData: t.detail
        });
    },
    onSelectShare: function(t) {
        this.onCloseShare(), 0 == t.detail.index && this.setData({
            posterShow: !0
        }), console.log(t.detail.index);
    },
    onCloseShare: function() {
        this.setData({
            shareShow: !1
        });
    },
    openShareShow: function() {
        this.setData({
            shareShow: !0
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    gotoBusinessInfo: function() {
        var t = this.data.group_id, a = this.data.deliver_id;
        wx.navigateTo({
            url: "/packageE/community_buying/business_info/business_info?buyingid=" + t + "&deliver_id=" + a
        });
    },
    getData: function() {
        var t = this, a = this, s = this.data.group_id, o = this.data.deliver_id, d = i.getNetAddresss("plugin.package-deliver.frontend.communityGroups.groupDetail");
        i._getNetWork({
            url: d,
            data: {
                id: s,
                deliver_id: o
            },
            success: function(i) {
                var s = i.data;
                if (1 !== s.result) return t.tips(s.msg);
                var o = s.data.group, d = s.data.deliver, r = 0;
                0 == o.group_status ? r = new Date(o.start_time.replace(/-/g, "/")).getTime() - new Date().getTime() : 1 == o.group_status && (r = new Date(o.end_time.replace(/-/g, "/")).getTime() - new Date().getTime()), 
                t.setData({
                    group: o,
                    time: r,
                    deliver: d
                });
                var n = s.data.group.description;
                try {
                    n.indexOf("&#") <= -1 && e.wxParse("article", "html", n, a);
                } catch (t) {
                    console.log(t);
                }
                o.color && t.setData({
                    themeColor: o.color
                }), t.getCartList();
            }
        });
    },
    getCartList: function() {
        var t = this, a = this.data.group_id, e = this.data.deliver.id;
        console.log(e, "deliver_id");
        var s = i.getNetAddresss("plugin.package-deliver.frontend.memberCart.index");
        i._postNetWork({
            url: s,
            data: {
                group_id: a,
                deliver_id: e
            },
            success: function(a) {
                var e = a.data;
                if (1 !== e.result) return t.tips(e.msg);
                var i = e.data;
                t.setData({
                    carList: i
                }), t.getFirstCart(i);
            }
        });
    },
    close_yz_cartList_popup: function() {
        this.setData({
            cartListShow: !1
        });
    },
    checkGoods: function() {
        this.submitGoods();
    },
    submitGoods: function() {
        if (this.data.showChoose) {
            this.data.cart_ids = [];
            for (var t = 0; t < this.data.dispatch_types[this.data.radioChoose].member_carts.length; t++) this.data.cart_ids.push(this.data.dispatch_types[this.data.radioChoose].member_carts[t].id);
        }
        if (0 != this.data.cart_ids.length) {
            var a = this.data.deliver;
            a.id, a.deliver_name, a.deliver_mobile, a.full_address;
            wx.navigateTo({
                url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=communityBuy&package_deliver_id=" + this.data.deliver.id + "&group_id=" + this.data.group_id + "&cart_ids=" + JSON.stringify(this.data.cart_ids)
            });
        }
    },
    openGoodsDetail: function(t) {
        var a = t.currentTarget.dataset.item;
        a.id == this.data.hand_goods_id ? this.setData({
            goodsPopupShow: !0
        }) : (this.data.hand_goods_id = a.id, this.getGoodsDetails(a.id, "goodsDetails"));
    },
    openCartListPopup: function() {
        this.setData({
            cartListShow: !this.data.cartListShow
        });
    },
    calculateTotal: function(t) {
        var a = {};
        for (var e in t) {
            var i = t[e].goods_id;
            this.data.cart_ids.push(t[e].id), a[i] ? a[i].total = a[i].total + t[e].total : (a[i] = {}, 
            a[i].goods_id = t[e].goods_id, a[i].total = t[e].total, a[i].id = t[e].id);
        }
        this.setData({
            goodsCartsTotal: a
        });
    },
    getFirstCart: function(t) {
        this.data.carts = t;
        var a = 0, e = 0;
        this.data.carts.forEach(function(t) {
            a += t.total, e += t.total * t.goods.price;
        }), e = parseFloat(e).toFixed(2), this.setData({
            cartsNum: a,
            cartsTotal: e
        }), this.calculateTotal(this.data.carts);
    },
    updateCartNum: function(t) {
        console.log(t);
        var a = t.target.dataset.item, e = t.target.dataset.cartid, i = t.detail.value, s = i;
        "" != i && 0 != i || (s = 0), this.data.goodsCartsTotal[a.id].total != s && this.reduceCartRequest({
            goods: a,
            cartid: e,
            total: s
        });
    },
    reduceCartNum: function(t) {
        var a = t.currentTarget.dataset.item, e = t.currentTarget.dataset.cartid;
        this.reduceCartRequest({
            goods: a,
            cartid: e
        });
    },
    reduceCartRequest: function(t) {
        var a = this, e = t.goods, s = t.cartid, o = t.total, d = e.id, r = 0;
        if (o || 0 === o) r = o; else {
            var n = this.data.goodsCartsTotal;
            r = Number(n[d].total) - 1, this.data.goodsCartsTotal[d].total = r;
        }
        var l = "", u = {};
        r <= 0 ? (l = i.getNetAddresss("plugin.package-deliver.frontend.memberCart.destroy"), 
        u = {
            ids: s
        }) : (l = i.getNetAddresss("plugin.package-deliver.frontend.memberCart.updateNumV2"), 
        u = {
            id: s,
            num: r
        }), this.setData({
            disabledInp: !0
        }), i._getNetWork({
            url: l,
            data: u,
            success: function(t) {
                var e = t.data;
                a.tips(e.msg), a.getCartList(), a.setData({
                    disabledInp: !1
                });
            }
        });
    },
    addCartNum: function(t) {
        var a = t.currentTarget.dataset.item, e = t.currentTarget.dataset.cartid;
        console.log(a), a.id = a.goods_id, this.addCartRequest({
            goods: a,
            cartid: e
        });
    },
    addCartRequest: function(t) {
        var a = this, e = t.goods, s = t.cartid, o = t.total, d = t.optionsId;
        if (console.log("加入购物车"), e.vip_level_status && 1 == e.vip_level_status.status) return this.tips(e.vip_level_status.tips);
        if (0 == e.stock) return this.tips("库存为零");
        var r = 1, n = "", l = {};
        if (i._isTextEmpty(this.data.goodsCartsTotal[e.id]) || 0 == s) n = i.getNetAddresss("plugin.package-deliver.frontend.memberCart.store"), 
        o && o > 0 && (r = o), l = {
            goods_id: e.id,
            total: r
        }, d && d > 0 && (l.option_id = d); else {
            if (this.data.goodsCartsTotal[e.id].total >= e.stock) return this.tips("库存不足");
            var u = this.data.goodsCartsTotal;
            r = Number(u[e.id].total) + 1, o && o > 0 && (r = o), this.data.goodsCartsTotal[e.id].total = r, 
            n = i.getNetAddresss("plugin.package-deliver.frontend.memberCart.updateNumV2"), 
            l = {
                id: s,
                num: r
            };
        }
        var c = this.data.deliver.id;
        l.group_id = this.data.group_id, l.deliver_id = c, i._getNetWork({
            url: n,
            data: l,
            success: function(t) {
                var e = t.data;
                a.tips(e.msg), 1 == e.result && a.getCartList();
            }
        });
    },
    cartListAdd: function(t) {
        console.log(t.detail), console.log(this.data.goodsCartsTotal), this.addCartRequest({
            goods: t.detail.goods,
            cartid: t.detail.id,
            total: t.detail.total + 1
        });
    },
    cartListReduce: function(t) {
        console.log(t.detail), this.reduceCartRequest({
            goods: t.detail.goods,
            cartid: t.detail.id,
            total: t.detail.total - 1
        });
    },
    goodsDetailCartAdd: function(t) {
        this.setData({
            goodsPopupShow: !1,
            specsShow: !0
        });
    },
    cartListInputBlur: function(t) {
        var e, i = this.data.carList, s = a(i);
        try {
            for (s.s(); !(e = s.n()).done; ) {
                var o = e.value;
                if (console.log(t), o.id == t.detail.id && o.total == t.detail.total) return;
            }
        } catch (t) {
            s.e(t);
        } finally {
            s.f();
        }
        this.reduceCartRequest({
            goods: t.detail.goods,
            cartid: t.detail.id,
            total: t.detail.total
        });
    },
    openSpecs: function(t) {
        var a = t.currentTarget.dataset.item;
        console.log(a), a.id == this.data.hand_goods_id ? this.setData({
            specsShow: !0
        }) : (this.data.hand_goods_id = a.id, this.getGoodsDetails(a.id, "specs"));
    },
    close_yz_specs_popup: function(t) {
        console.log(t), 1 == t.detail.clicktype ? (this.addCartRequest({
            goods: {
                id: t.detail.goodsId
            },
            cartid: t.detail.cartid,
            total: t.detail.goodsCount,
            optionsId: t.detail.optionsId
        }), this.setData({
            specsShow: !1
        })) : this.setData({
            specsShow: !1
        });
    },
    getGoodsDetails: function(a, e) {
        var s = this, o = i.getNetAddresss("goods.goods.get-goods-page");
        i._postNetWork({
            url: o,
            data: {
                id: Number(a)
            },
            success: function(a) {
                var i, o = a.data;
                if (1 !== o.result) return s.tips(o.msg);
                var d = "";
                "goodsDetails" == e ? d = "goodsPopupShow" : "specs" == e && (d = "specsShow"), 
                o.data.get_goods.end_time = s.data.time, s.setData((t(i = {}, d, !0), t(i, "hand_goods_obj", o.data.get_goods), 
                i));
            }
        });
    },
    tips: function(t) {
        wx.showToast({
            title: t,
            icon: "none"
        });
    },
    onShareAppMessage: function() {
        var t = "/packageE/community_buying/buying_personal/buying_personal?buyingid=" + this.data.group_id + "&deliver_id=" + this.data.deliver_id, a = wx.getStorageSync("yz_uid");
        a && (t = t + "&mid=" + a);
        var e = {
            path: t
        };
        return this.data.group.share_img && (e.imageUrl = this.data.group.share_img), console.log(e), 
        e;
    }
});